#include "SocketListenerTask.h"
#include "Socket.h"
#include "Thread.h"
#include "ClientHandlerTask.h"
#include "HTTPHandler.h"
SK

SocketListenerTask::SocketListenerTask (JarvCore* core, u16 localPort)
:lPort (localPort), core(core)
{
	soc = new Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
}

SocketListenerTask::~SocketListenerTask ()
{
	if (soc)
		delete soc;
}


void SocketListenerTask::run (void)
{
	running = true; //maybe move running to super class?
	soc->setTimeout(5000);	//5secs
	soc->bind (lPort);
	soc->listen();

	while (running)
	{
		Socket *tmpSoc = soc->accept ();

		if (tmpSoc)
		{
			Thread *t = new Thread(NULL);
			t->start(new ClientHandlerTask (core, tmpSoc));
		}
		else
		{
			running = false;
		}
	}
	soc->close();
}

EK